CLASS lhc_main DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    TYPES:
      tt_main   TYPE STANDARD TABLE OF ymoont_api001m,
      tt_detail TYPE STANDARD TABLE OF ymoont_api001d.

    METHODS call_out_api EXPORTING out_main   TYPE tt_main
                                   out_detail TYPE tt_detail
                                   search_str TYPE string.

    METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
      IMPORTING keys REQUEST requested_authorizations FOR main RESULT result.
    METHODS call_api FOR MODIFY
      IMPORTING keys FOR ACTION main~call_api.

ENDCLASS.

CLASS lhc_main IMPLEMENTATION.

  METHOD get_instance_authorizations.
  ENDMETHOD.

  METHOD call_api.
    DATA: lt_main TYPE tt_main.
    DATA: lt_main_temp TYPE tt_main.
    DATA: lt_detail TYPE tt_detail.
    DATA: lt_detail_temp TYPE tt_detail.


    DATA:lt_crt_main TYPE TABLE FOR CREATE ymoon_i_api001m\\main,
         ls_crt_main LIKE LINE OF lt_crt_main.
    DATA:lt_crt_detail TYPE TABLE FOR CREATE ymoon_i_api001m\_detail,
         ls_crt_detail LIKE LINE OF lt_crt_detail.


    FIELD-SYMBOLS:<fs> TYPE any.

    DATA(key) = keys[ 1 ].
    DATA(param) = key-%param.

    call_out_api( IMPORTING out_main = lt_main out_detail = lt_detail search_str = param-cargmtno ).

    IF lt_main IS NOT INITIAL.
*      SELECT uuid FROM ymoont_api001m  WHERE cargmtno = @param-cargmtno INTO TABLE @DATA(lt_uuid).
      SELECT uuid FROM ymoont_api001m INTO TABLE @DATA(lt_uuid).
      LOOP AT lt_uuid INTO DATA(ls_uuid).

        MODIFY ENTITIES OF ymoon_i_api001m IN LOCAL MODE
        ENTITY main
        DELETE FROM VALUE #( ( uuid = ls_uuid-uuid ) ).

        SELECT uuid FROM ymoont_api001d INTO TABLE @DATA(lt_uuid_detail).
        LOOP AT lt_uuid_detail INTO DATA(ls_uuid_detail).
          MODIFY ENTITIES OF ymoon_i_api001m IN LOCAL MODE
              ENTITY detail
              DELETE FROM VALUE #( ( uuid = ls_uuid_detail-uuid ) ).
        ENDLOOP.

      ENDLOOP.

      LOOP AT lt_main INTO DATA(ls_main).
        MOVE-CORRESPONDING ls_main TO ls_crt_main.
        ls_crt_main-%cid = cl_system_uuid=>create_uuid_x16_static( ).
        ls_crt_main-uuid = ls_crt_main-%cid.
        ls_crt_main-regid = sy-uname.
        ls_crt_main-regdate = cl_abap_context_info=>get_system_date( ).

        CLEAR lt_crt_main.


        DO.
          ASSIGN COMPONENT sy-index OF STRUCTURE ls_crt_main-%control TO <fs>.
          IF sy-subrc = 0.
            <fs> = '01'.
          ELSE.
            EXIT.
          ENDIF.
        ENDDO.

        APPEND ls_crt_main TO lt_crt_main.


*          APPEND VALUE #( %cid_ref = ls_crt_main-%cid uuid = ls_crt_main-uuid )
*  TO lt_crt_detail ASSIGNING FIELD-SYMBOL(<detail>).


        ls_crt_detail-%cid_ref = ls_crt_main-%cid.
        ls_crt_detail-uuid = ls_crt_main-uuid.


        LOOP AT lt_detail INTO DATA(ls_detail).
          ls_detail-regid = sy-uname.
          ls_detail-regdate = cl_abap_context_info=>get_system_date( ).

          APPEND VALUE #( %cid  = cl_system_uuid=>create_uuid_x16_static( )
                          uuid = cl_system_uuid=>create_uuid_x16_static( )
                          puuid = ls_crt_main-uuid
                          %data = CORRESPONDING #( ls_detail EXCEPT uuid puuid )
                          )
                       TO ls_crt_detail-%target ASSIGNING FIELD-SYMBOL(<detail_control>).

          DO.
            CHECK sy-index > 2.
            ASSIGN COMPONENT sy-index OF STRUCTURE <detail_control>-%control TO <fs>.
            IF sy-subrc = 0.
              <fs> = '01'.
            ELSE.
              EXIT.
            ENDIF.
          ENDDO.


        ENDLOOP.

        APPEND ls_crt_detail TO lt_crt_detail.

        MODIFY ENTITIES OF ymoon_i_api001m IN LOCAL MODE
        ENTITY main
        CREATE FROM lt_crt_main
        ENTITY main
        CREATE BY \_detail
        FROM lt_crt_detail.
      ENDLOOP.
    ENDIF.

  ENDMETHOD.


  METHOD call_out_api.
    DATA:http_client TYPE REF TO ymoon_api.
    CREATE OBJECT http_client
      EXPORTING
        i_scenario     = 'YMOON_CS_API001'
        i_service      = 'YMOON_OS_API001_REST'
      EXCEPTIONS
        no_arrangement = 1.
    IF sy-subrc = 1.
      "out->write( 'no arrangement' ).
    ENDIF.
    CHECK sy-subrc <> 1.

    DATA param TYPE string.
    DATA res TYPE string.
    param = 'crkyCn=y220j168m038w239m000t080n2&cargMtNo=' && search_str.

    res = http_client->get( param ).


    DATA: lv_xstring TYPE xstring.
    DATA: lv_conv TYPE REF TO cl_abap_conv_codepage.
    lv_xstring = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( source = res ).
    "out->write( lv_xstring ).

*    DATA:BEGIN OF ls_data,
*           cntysgn   TYPE c  LENGTH 2,
*           mtryutnm  TYPE c  LENGTH 30,
*           fxrt      TYPE c  LENGTH 13,
*           currsgn   TYPE c  LENGTH 3,
*           aplybgndt TYPE c  LENGTH 8,
*           imextp    TYPE c  LENGTH 1,
*         END OF ls_data.
*    DATA:lt_data LIKE TABLE OF ls_data.

    DATA:lt_data TYPE tt_main,
         ls_data TYPE LINE OF tt_main.
    DATA:lt_data_detail TYPE tt_detail,
         ls_data_detail TYPE LINE OF tt_detail.

    DATA:gv_cond TYPE c LENGTH 50 VALUE 'cargCsclPrgsInfoQryVo'.
    DATA:gv_cond2 TYPE c LENGTH 50 VALUE 'cargCsclPrgsInfoDtlQryVo'.

    DATA:gv_field TYPE c LENGTH 200.
    DATA:gv_field2 TYPE c LENGTH 200.

    FIELD-SYMBOLS <fs> TYPE any.
    FIELD-SYMBOLS <fs2> TYPE any.


    DATA: lv_reader          TYPE REF TO if_sxml_reader.
    DATA: lv_node            TYPE REF TO if_sxml_node.
    DATA: lv_open_node       TYPE REF TO if_sxml_open_element.
    DATA: lv_close_node      TYPE REF TO if_sxml_close_element.
    DATA: lv_value_node      TYPE REF TO if_sxml_value.
    DATA: lv_value           TYPE string.

    lv_reader = cl_sxml_string_reader=>create( lv_xstring ).

    lv_node = lv_reader->read_next_node( ).


    ASSERT lv_node IS NOT INITIAL.
    WHILE lv_reader->node_type <> if_sxml_node=>co_nt_final.
      CASE lv_reader->node_type.
        WHEN if_sxml_node=>co_nt_element_open.
          lv_open_node  ?= lv_node.

          gv_field = 'ls_data-' && lv_open_node->qname-name.
          gv_field2 = 'ls_data_detail-' && lv_open_node->qname-name.

          IF lv_open_node->qname-name = 'prcsDttm'.
            gv_field2 = 'ls_data_detail-' && lv_open_node->qname-name.
          ENDIF.

        WHEN if_sxml_node=>co_nt_element_close.
          lv_close_node ?= lv_node.

          IF lv_close_node->qname-name = gv_cond.
            APPEND ls_data TO lt_data.
            CLEAR ls_data.
          ELSE.
            ASSIGN (gv_field) TO <fs>.
            IF sy-subrc = 0.
              <fs> = lv_value.

            ENDIF.
          ENDIF.


          IF lv_close_node->qname-name = gv_cond2.
            APPEND ls_data_detail TO lt_data_detail.
            CLEAR ls_data_detail.
          ELSE.
            ASSIGN (gv_field2) TO <fs2>.
            IF sy-subrc = 0.
              <fs2> = lv_value.

            ENDIF.
          ENDIF.


          CLEAR lv_value.

        WHEN if_sxml_node=>co_nt_value.
          lv_value_node ?= lv_node.
          CLEAR lv_value.
          lv_value = lv_value_node->get_value( ).
        WHEN if_sxml_node=>co_nt_attribute.
      ENDCASE.
      lv_node = lv_reader->read_next_node( ).
    ENDWHILE.

    MOVE-CORRESPONDING lt_data TO out_main .
    MOVE-CORRESPONDING lt_data_detail TO out_detail .



  ENDMETHOD.

ENDCLASS.
